// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.portal.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fowo.api.common.model.PageSearch;
import com.fowo.api.common.model.R;
import com.fowo.api.portal.entity.Desktop;
import com.fowo.api.portal.model.DesktopAddUpdateVo;
import com.fowo.api.portal.model.DesktopSearchParamPo;
import com.fowo.api.portal.model.DesktopVo;
import com.fowo.api.portal.service.DesktopService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Tag(name = "门户表")
@RequestMapping("/api/desktop")
@RestController
public class DesktopController {


    @Resource
    private DesktopService desktopService;


    @Operation(summary = "查询结果")
    @GetMapping("/search")
    public R<IPage<Desktop>> search(
            DesktopSearchParamPo search
    ) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        search.setCurrentUserId(jwtUserInfo.getUserId());
        return R.data(desktopService.pageSearch(search));
    }


    @Operation(summary = "获取详情")
    @GetMapping("/getById/{id}")
    public R<DesktopVo> getVoById(@PathVariable Long id) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.data(() -> desktopService.getVoById(id));
    }


//    @PreAuthorize("@am.canAccess('desktopCreate')")
    @Operation(summary = "新增")
    @PostMapping("/create")
    public R<Long> create(@Validated @RequestBody DesktopAddUpdateVo model) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.data(() -> desktopService.create(model));
    }


//    @PreAuthorize("@am.canAccess('desktopEdit')")
    @Operation(summary = "更新")
    @PostMapping("/update")
    public R update(@RequestBody DesktopAddUpdateVo vo) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.run(() -> desktopService.updateEdit(vo));
    }

    @Operation(summary = "发布版本")
    @PostMapping("/publishScheme")
    public R publishScheme(@RequestBody DesktopAddUpdateVo vo) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.run(() -> desktopService.publishScheme(vo));
    }


//    @PreAuthorize("@am.canAccess('desktopDelete')")
    @Operation(summary = "删除")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.run(() -> desktopService.delete(id));
    }


    /**
     * 分页查询历史数据
     */
    @Operation(summary = "查询历史数据")
    @GetMapping("/history/{id}")
    public R<IPage<Desktop>> getHistoryPage(@PathVariable Long id, PageSearch pageSearch){
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        pageSearch.setCurrentUserId(jwtUserInfo.getUserId());
        return R.data(desktopService.getHistoryPage(id,pageSearch));
    }

    /**
     * 复制
     *
     * @param
     * @return
     */
    @Operation(summary = "复制门户")
    @GetMapping("/CopyDeskTop/{id}")
    public R CopyDeskTop(@PathVariable Long id) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.run(() -> desktopService.CopyDeskTop(id));
    }


    /**
     * 切换版本
     *
     * @return
     */
    @Operation(summary = "切换版本")
    @GetMapping("/switchVersion/{id}")
    public R switchVersion(@PathVariable Long id) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.run(()->desktopService.switchVersion(id));
    }




    /**
     * 预览
     *
     * @return
     */
    @Operation(summary = "预览")
    @GetMapping("/PreviewDeskSchemeJson/{id}")
    private R PreviewDeskSchemeJson(@PathVariable Long id){
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        return R.data(desktopService.previewDeskSchemeJson(id));
    }
}
